<?php
declare(strict_types=1);

namespace App\Logic;

use App\Event\AdministratorLogsEvent;
use App\Models\ArticleCategory as ArticleCategoryModel;

class ArticleCategory
{

    public function count(array $where = []): int
    {
        $map[] = ['delete_time', '=', 0];
        if (!empty($where)) {
            if (!empty($where['cate_name'])) $map[] = ['cate_name', 'like',  '%'.$where['cate_name'].'%'];
        }
        return ArticleCategoryModel::where($map)->count();
    }

    public function list(int $start, int $limit, array $where = [])
    {
        $map[] = ['delete_time', '=', 0];
        if (!empty($where)) {
            if (!empty($where['cate_name'])) $map[] = ['cate_name', 'like',  '%'.$where['cate_name'].'%'];
        }
        return ArticleCategoryModel::where($map)->orderBy('id','desc')->offset($start)->limit($limit)->get();
    }

    public function add(array $data)
    {
        $model = new ArticleCategoryModel();
        foreach ($data as $k => $v) {
            $model->$k = $v;
        }
        if ($model->save($data)){
            event(new AdministratorLogsEvent("添加文章类别"));
            return true;
        }
        return false;
    }

    public function update(int $id, array $data)
    {
        $model = ArticleCategoryModel::find($id);
        foreach ($data as $k => $v) {
            $model->$k = $v;
        }
        if ($model->save($data)){
            event(new AdministratorLogsEvent("修改文章类别"));
            return true;
        }
        return false;
    }

    public function status(int $id)
    {
        $model = ArticleCategoryModel::find($id);
        if (!empty($model)) {
            if ($model->cate_status == 1){
                $model->cate_status = 2;
            }else{
                $model->cate_status = 1;
            }
            if ($model->save()){
                event(new AdministratorLogsEvent("修改文章类别状态"));
                return true;
            }
        }
        return false;
    }

    public function delete(int $id)
    {
        $model = ArticleCategoryModel::find($id);
        if ($model->delete()){
            event(new AdministratorLogsEvent("删除文章类别"));
            return true;
        }
        return false;
    }

    public function find(int $id)
    {
        return ArticleCategoryModel::find($id);
    }

    public function has(int $id)
    {
        $model = ArticleCategoryModel::find($id);
        if (empty($model)) return false;
        return true;
    }

    public function checkName(string $name, int $id=0)
    {
        $model = ArticleCategoryModel::where('cate_name',$name)->where('id','<>',$id)->first();
        if (empty($model)) return true;
        return false;
    }

    public function select(array $where = [])
    {
        $map[] = ['cate_status', '=', 1];
        $map[] = ['delete_time', '=', 0];
        if (!empty($where)) {
            if (!empty($where['cate_name'])) $map[] = ['cate_name', 'like',  '%'.$where['cate_name'].'%'];
        }
        return ArticleCategoryModel::select('id','cate_name')->where($map)->orderBy('id','desc')->get();
    }

}
